package com.syscom.admin.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.syscom.common.entity.SysMenu;
import com.syscom.common.enums.UniversalStatus;
import com.syscom.common.mapper.SysMenuMapper;
import com.syscom.common.utils.result.AjaxResult;
import com.syscom.common.utils.result.ResultTool;
import com.syscom.core.utils.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @author wjz
 * @date 2020-12-28
 */
@Slf4j
@Service
public class SysMenuService {

    @Autowired
    private SysMenuMapper sysMenuMapper;

    @Autowired
    private SecurityUtils securityUtils;

    public AjaxResult queryAllMenu() {

        QueryWrapper<SysMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("object_id", "name", "order_no", "parent_id")
                .eq("enabled", UniversalStatus.YES)
                .eq("deleted", UniversalStatus.NO)
                .orderByAsc("order_no,object_id");
        List<SysMenu> menuList = sysMenuMapper.selectList(queryWrapper);
        List child = new ArrayList();
        List parent = new ArrayList();
        for (SysMenu sysMenu : menuList) {
            if (sysMenu.getParentId() != null) {
                child.add(sysMenu);
            } else {
                parent.add(sysMenu);
            }
        }
        return ResultTool.success(new HashMap(2) {{
            put("Child", child);
            put("Parent", parent);
        }});
    }

    public AjaxResult queryMenuById(Long objectId) {

        return ResultTool.success(sysMenuMapper.selectById(objectId));
    }

    public AjaxResult addSysMenu(SysMenu sysMenu) {

        sysMenu.setCreatedDate(new Date());
        sysMenu.setCreatedBy(securityUtils.getCurrentUserId());
        sysMenu.setDeleted(UniversalStatus.NO);
        sysMenu.setHidden(UniversalStatus.NO);
        return ResultTool.success(sysMenuMapper.insert(sysMenu));
    }

    public AjaxResult updateSysMenu(SysMenu sysMenu) {

        sysMenu.setLastUpdateDate(new Date());
        sysMenu.setLastUpdateBy(securityUtils.getCurrentUserId());
        return ResultTool.success(sysMenuMapper.updateById(sysMenu));
    }

    public AjaxResult deleteSysMenu(Long objectId) {

        SysMenu sysMenu = sysMenuMapper.selectOne(new QueryWrapper<SysMenu>()
                .eq("object_id", objectId)
                .isNull("parent_id")
        );
        if (sysMenu != null) {
            sysMenuMapper.update(
                    new SysMenu().setDeleted(UniversalStatus.YES),
                    new QueryWrapper<SysMenu>().eq("parent_id", objectId)
            );
        }else {
            sysMenu = new SysMenu();
        }
        sysMenuMapper.update(sysMenu.setDeleted(UniversalStatus.YES), new UpdateWrapper<SysMenu>().eq("object_id", objectId));
        return ResultTool.success();
    }
}

